package com.twodstar.photobattle.slidemenu;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.twodstar.photobattle.R;



public class BaseActivity extends SlidingFragmentActivity 
{

	protected Fragment mContent;
	private int mTitleRes;
	protected SlideMenuFragment mFrag;
	
		
	
	@Override
	public void onSaveInstanceState(Bundle outState) 
	{
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}

	public BaseActivity() 
	{
		mTitleRes = R.string.app_name;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(mTitleRes);

		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		if (savedInstanceState == null) 
		{
			FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
			
			mFrag = new SlideMenuFragment();
			
			t.replace(R.id.menu_frame, mFrag);
			t.commit();
		} else {
			mFrag = (SlideMenuFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
		}

		// customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		// set the Above View
				if (savedInstanceState != null)
					mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
				
				if (mContent == null)
					mContent = new SherlockFragment();	
				
				// set the Above View
				setContentView(R.layout.content_frame);
				
				getSupportFragmentManager()
				.beginTransaction()		
					.replace(R.id.content_frame, mContent)
				.commit();
				
				// set the Behind View
				setBehindContentView(R.layout.menu_frame);
				getSupportFragmentManager()
				.beginTransaction()
				.replace(R.id.menu_frame, new SlideMenuFragment())
				.commit();
				
				// customize the SlidingMenu
				getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
				
				
				getSupportActionBar().setStackedBackgroundDrawable(getResources().getDrawable( R.drawable.title_bar_background ));
				getSupportActionBar().setBackgroundDrawable(getResources().getDrawable( R.drawable.title_bar_background )); 
				getSupportActionBar().setDisplayShowTitleEnabled(false);		  
				getSupportActionBar().setIcon(R.drawable.mondobar_icon_launcher_normal);
				
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			return true;
		
		}
		return super.onOptionsItemSelected(item);
	}
	
	

	
}
